package cn.edu.hunau.java.week03.day03;

import cn.edu.hunau.java.week03.day02.Rice;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.*;

public class RiceTest {

    @Test
    public void testRice(){
        Map<String,Map<String,Double>> map=parseRice(get50RiceRecords());
        System.out.println(map);
    }

    public Map<String,Map<String,Double>> parseRice(List<Rice> rices){
        Map<String,Map<String,Double>> map=new HashMap<>();
        Map<String,List<Double>> yieldPerAcre=new HashMap<>();
        for(Rice rice:rices){
            String variety=rice.getVariety();
            List<Double> list=yieldPerAcre.getOrDefault(variety,new ArrayList<>());
            list.add(rice.getYieldPerAcre());
            yieldPerAcre.put(variety,list);
        }
        Set<Map.Entry<String,List<Double>>> entries=yieldPerAcre.entrySet();
        for(Map.Entry<String,List<Double>> entry:entries){
            String variety=entry.getKey();
            List<Double> list=entry.getValue();
            list.sort((a,b)->a.compareTo(b));
            Map<String,Double> map1=new HashMap<>();
            map1.put("min", list.getFirst());
            map1.put("max", list.getLast());

            double sum=0;
            for(Double d:list){
                sum+=d;
            }
            double avg=sum/list.size();
            BigDecimal bd=new BigDecimal(avg);
            BigDecimal newBD=bd.setScale(2, BigDecimal.ROUND_HALF_UP);

            map1.put("avg", newBD.doubleValue());
            map.putIfAbsent(variety,map1);
        }
        System.out.println(yieldPerAcre);
        return map;
    }

    public List<Rice> get50RiceRecords() {
        List<Rice> riceRecords = new ArrayList<>();

        // 记录1-10
        riceRecords.add(createRice("龙粳31", "黑龙江", 0.95, 128, true, 580.2, "深绿色"));
        riceRecords.add(createRice("南粳9108", "江苏", 1.12, 135, false, 620.5, "绿色"));
        riceRecords.add(createRice("黄华占", "广东", 1.05, 120, true, 650.8, "绿色"));
        riceRecords.add(createRice("晶两优534", "湖南", 1.20, 132, true, 680.3, "深绿色"));
        riceRecords.add(createRice("中嘉早17", "江西", 1.08, 115, false, 590.6, "绿色"));
        riceRecords.add(createRice("荃优822", "湖北", 1.18, 130, true, 670.1, "深绿色"));
        riceRecords.add(createRice("宜香优2115", "云南", 1.25, 140, false, 630.4, "绿色"));
        riceRecords.add(createRice("美香占2号", "广西", 1.02, 122, true, 570.7, "深绿色"));
        riceRecords.add(createRice("绥粳27", "黑龙江", 0.92, 125, false, 560.9, "绿色"));
        riceRecords.add(createRice("甬优1540", "浙江", 1.30, 138, true, 690.2, "深绿色"));

        // 记录11-20
        riceRecords.add(createRice("龙粳31", "黑龙江", 0.98, 126, true, 585.3, "绿色"));
        riceRecords.add(createRice("南粳9108", "江苏", 1.10, 133, false, 615.7, "深绿色"));
        riceRecords.add(createRice("黄华占", "海南", 1.03, 118, true, 645.9, "绿色"));
        riceRecords.add(createRice("晶两优534", "安徽", 1.19, 131, true, 675.4, "深绿色"));
        riceRecords.add(createRice("中嘉早17", "湖北", 1.07, 116, false, 588.2, "绿色"));
        riceRecords.add(createRice("荃优822", "河南", 1.17, 129, true, 668.5, "深绿色"));
        riceRecords.add(createRice("宜香优2115", "四川", 1.23, 139, false, 628.8, "绿色"));
        riceRecords.add(createRice("美香占2号", "广东", 1.01, 121, true, 568.3, "深绿色"));
        riceRecords.add(createRice("绥粳27", "黑龙江", 0.90, 124, false, 558.6, "绿色"));
        riceRecords.add(createRice("甬优1540", "福建", 1.28, 137, true, 685.7, "深绿色"));

        // 记录21-30
        riceRecords.add(createRice("龙粳31", "黑龙江", 0.96, 127, true, 582.5, "深绿色"));
        riceRecords.add(createRice("南粳9108", "江苏", 1.11, 134, false, 618.9, "绿色"));
        riceRecords.add(createRice("黄华占", "湖南", 1.04, 119, true, 648.1, "深绿色"));
        riceRecords.add(createRice("晶两优534", "江西", 1.21, 133, true, 682.6, "绿色"));
        riceRecords.add(createRice("中嘉早17", "浙江", 1.09, 117, false, 592.4, "深绿色"));
        riceRecords.add(createRice("荃优822", "重庆", 1.18, 130, true, 672.8, "绿色"));
        riceRecords.add(createRice("宜香优2115", "陕西", 1.24, 141, false, 632.1, "深绿色"));
        riceRecords.add(createRice("美香占2号", "广西", 1.03, 123, true, 572.5, "绿色"));
        riceRecords.add(createRice("绥粳27", "黑龙江", 0.93, 126, false, 562.7, "深绿色"));
        riceRecords.add(createRice("甬优1540", "上海", 1.29, 139, true, 688.9, "绿色"));

        // 记录31-40
        riceRecords.add(createRice("龙粳31", "黑龙江", 0.97, 129, true, 587.6, "绿色"));
        riceRecords.add(createRice("南粳9108", "江苏", 1.13, 136, false, 622.3, "深绿色"));
        riceRecords.add(createRice("黄华占", "江西", 1.06, 121, true, 652.4, "绿色"));
        riceRecords.add(createRice("晶两优534", "湖北", 1.22, 134, true, 684.7, "深绿色"));
        riceRecords.add(createRice("中嘉早17", "安徽", 1.10, 118, false, 594.6, "绿色"));
        riceRecords.add(createRice("荃优822", "云南", 1.19, 131, true, 674.9, "深绿色"));
        riceRecords.add(createRice("宜香优2115", "贵州", 1.26, 142, false, 635.2, "绿色"));
        riceRecords.add(createRice("美香占2号", "海南", 1.04, 124, true, 574.8, "深绿色"));
        riceRecords.add(createRice("绥粳27", "黑龙江", 0.94, 127, false, 564.9, "绿色"));
        riceRecords.add(createRice("甬优1540", "浙江", 1.31, 140, true, 692.3, "深绿色"));

        // 记录41-50
        riceRecords.add(createRice("龙粳31", "黑龙江", 0.99, 130, true, 589.8, "深绿色"));
        riceRecords.add(createRice("南粳9108", "江苏", 1.14, 137, false, 625.5, "绿色"));
        riceRecords.add(createRice("黄华占", "湖北", 1.07, 122, true, 655.7, "深绿色"));
        riceRecords.add(createRice("晶两优534", "河南", 1.23, 135, true, 686.8, "绿色"));
        riceRecords.add(createRice("中嘉早17", "湖南", 1.11, 119, false, 596.9, "深绿色"));
        riceRecords.add(createRice("荃优822", "四川", 1.20, 132, true, 676.1, "绿色"));
        riceRecords.add(createRice("宜香优2115", "重庆", 1.27, 143, false, 637.4, "深绿色"));
        riceRecords.add(createRice("美香占2号", "广东", 1.05, 125, true, 576.3, "绿色"));
        riceRecords.add(createRice("绥粳27", "黑龙江", 0.95, 128, false, 566.5, "深绿色"));
        riceRecords.add(createRice("甬优1540", "福建", 1.32, 141, true, 695.6, "绿色"));

        return riceRecords;
    }

    // 辅助方法：创建水稻对象
    private   Rice createRice(String variety, String origin, double height, int growthDays, boolean isHybrid, double yield, String color) {
        Rice rice = new Rice(variety, origin, height, growthDays);
        rice.setHybrid(isHybrid);
        rice.setYieldPerAcre(yield);
        rice.setColor(color);
        // 移除了所有与生长阶段相关的代码
        return rice;
    }
}
